# Create your views here.
from edg.acm import ConfigurationWorker as ConfigWorker
from django.http import HttpResponse
from edgdevelopment.inputer.models import sim_forms
from django.contrib.auth.models import User
#from edgdevelopment.fakemodule2.module2 import ConfigModel
from edgdevelopment.reporter.models import RunID
from django.contrib import auth

from edg.acm import ConfigurationModel as ConfigModel

from django import forms
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

def input_form(request):
        if request.method == 'POST': # If the form has been submitted...
                form = sim_forms(request.POST) # A form bound to the POST data
                if form.is_valid(): # All validation rules pass
                        # Process the data in form.cleaned_data
                        # ... 
                        doctor = form.cleaned_data['doctor']
                        nurse = form.cleaned_data['nurse']
                        external_triage = form.cleaned_data['external_triage']
                        internal_triage = form.cleaned_data['internal_triage']
                        external_clerk = form.cleaned_data['external_clerk']
                        internal_clerk = form.cleaned_data['internal_clerk']
                        bed = form.cleaned_data['bed']
                        lab_tech = form.cleaned_data['lab_tech']
                        ctscan_tech = form.cleaned_data['ctscan_tech']
                        xray_tech = form.cleaned_data['xray_tech']
                        ultrasound_tech = form.cleaned_data['ultrasound_tech']
                        rate = form.cleaned_data['rate']
                        runs = form.cleaned_data['runs']
                        hours = form.cleaned_data['hours']
                        acuity1 = form.cleaned_data['acuity1']
                        acuity2 = form.cleaned_data['acuity2']
                        acuity3 = form.cleaned_data['acuity3']
                        acuity4 = form.cleaned_data['acuity4']
                        acuity5 = form.cleaned_data['acuity5']
                        acuity6 = form.cleaned_data['acuity6']
                        walkin = form.cleaned_data['walkin']
                        
                #change: the following 2 lines may cause wasted runID numbers 
                configuration = ConfigModel()
                configuration = configuration.createNewConfigModel()
                
        #       if request.user.is_authenticated:
        #        configuration.setUser(1)# 'request.user.userID)
        #        if 1 != None : #change 1 to request.user.runID equivalent
        #          configuration.setID(1)#request.user.runID)
        
                #KLUDGE!
                #When AnonymousUser is logged in, user.id = None
                #But ConfigModel.setID() expects an int
                #Solution:  AnonymousUser = 1
                #Other users: user.id+1
                print request.user.username
                if request.user.is_authenticated():
                        print "Auth'd user"
                        userid = request.user.id+1
                        print "auth'd: %s" % userid
                        user = request.user
                else: #anonymous
                        #bit of another kludge here until users are fully implemented...
                        print "Got anon in form"
                        user = auth.authenticate(username='AnonymousUser', password='anon')
                        auth.login(request, user)
                        #user = request.user
                        userid = 1
                        
                configuration.setUser(userid)
                
                parameters = { 
                'doctor':doctor,
                'nurse':nurse,
                'external triage':external_triage,
                'internal triage':internal_triage,
                'external clerk':external_clerk,
                'internal clerk':internal_clerk,
                'bed':bed,
                'lab tech':lab_tech,
                'ctscan tech':ctscan_tech,
                'xray tech':xray_tech,
                'ultrasound tech':bed,
                'rate':rate,
                'runs':runs,
                'hours':hours,
                'acuity1':acuity1,
                'acuity2':acuity2,
                'acuity3':acuity3,
                'acuity4':acuity4,
                'acuity5':acuity5,
                'acuity6':acuity6,
                'walkin':walkin,
                }
                
                counter = 0
                for item in parameters :
                        configuration.setValue(parameters.keys()[counter], parameters[item])
                        counter = counter + 1
                        
                cw = ConfigWorker()
                runid = cw.runConfiguration(configuration)
                
                #save this Run in module 4
                r = RunID(name="Run %s" % runid, mod3RunID = runid, user=user)
                #r.name = "Run %s" % runid
                #r.mod3RunID = runid
                #if request.user.is_anonymous():
                #       r.user = user
                #else:
                #       r.user = request.user
                r.save()
                
                
                #if request.user.is_anonymous():
                #       r.id = 1
                print r.id
                #thisrun = user.runid_set.all()[len(user.runid_set.all())-1]
                
                return HttpResponseRedirect('/reporter/results/%s' % r.id) # Redirect after POST
                
        else: #request.method != 'POST'  
          # if request.user.is_authenticated and 1 != None : #change 1 to request.user.runID equivalent 
          #   configuration = ConfigModel()
          #   configuration = configuration.loadCM(1)
          #   form = sim_forms(configuration.getCM())
          # else:
          form = sim_forms() # An unbound form
          
        return render_to_response('inputer/form.html', {'form': form, 'user':request.user})
  
def home(request):
        user = request.user
        return render_to_response('inputer/welcome.html', locals())
